<template>
  <el-form :model="form" :rules="rules" ref="form" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名/手机" v-model="form.username" @focus="clearValidate('username')"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        placeholder="密码"
        type="password"
        v-model="form.password"
        @focus="clearValidate('password')"
      ></el-input>
    </el-form-item>

    <p class="form-text">
      <nuxt-link to="#">忘记密码</nuxt-link>
    </p>

    <el-button class="submit" type="primary" @click="handleLoginSubmit">登录</el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据
      form: {
        username: "13800138000",
        password: "123456"
      },
      // 表单规则
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名或手机号",
            trigger: "blur"
          },
          {
            min: 1,
            max: 11,
            message: "长度在 1 到 11 个字符",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          },
          {
            pattern: /^\d{5,8}$/,
            message: "密码由5到8位数字组成",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    console.log(this.$route);
  },
  methods: {
    // 提交登录
    handleLoginSubmit() {
      // 验证表单为空时按提交
      this.$refs.form.validate((isValid, valid) => {
        // console.log(isValid);
        // console.log(valid);
        if (isValid) {
          // this.$axios({
          //   url: "/accounts/login",
          //   method: "post",
          //   data: this.form
          // }).then(res => {
          //   // console.log(res.data);
          //   //登录成功之后将数据store存储
          //   this.$store.commit("user/setUserInfo", res.data);
          //   // console.log(this.$store.state.user.userInfo);
          //   // 成功提示
          //   this.$message({
          //     message: "登录成功，正在跳转",
          //     type: "success"
          //   });
          //   // 跳转到首页
          //   setTimeout(() => {
          //     this.$router.replace("/");
          //   }, 1000);
          // });
          this.$store.dispatch("user/login", this.form).then(res => {
            // 成功提示
            this.$message({
              message: "登录成功，正在跳转",
              type: "success"
            });
            // 判断如果在登录页，登录成功就跳转到首页
            if (this.$route.path == "/user/login") {
              setTimeout(() => {
                // 返回上一页
                this.$router.go(-1)
              }, 800);
            }
          });
        }
      });
    },
    clearValidate(prop) {
      this.$refs.form.clearValidate(prop);
    }
  }
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>
